package com.zjw.hyphenateui.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.havi.bean.HyChatUser;
import com.example.havi.interfaces.LoginCallBack;
import com.example.havi.util.ChatSPUtil;
import com.zjw.hyphenateui.MainApplication;
import com.zjw.hyphenateui.R;
import com.zjw.hyphenateui.bean.UserBean;
import com.zjw.hyphenateui.helper.UserHelper;
import com.zjw.hyphenateui.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.editText2)
    EditText editText2;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.progress_fl)
    RelativeLayout progressFl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String chat_user = ChatSPUtil.getString("chat_user","");
        //同步用户账号与环信账号信息 项目使用中应该判断是否登录
        if(!TextUtils.isEmpty(chat_user)){
           startActivity(new Intent(getBaseContext(),MainActivity.class));
           finish();
           return;
        }
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.button)
    public void onViewClicked() {
        final String user = editText.getText().toString();
        if (user.isEmpty()) {
            ToastUtil.showToast(this, "请输入与用户名");
            return;
        }
        final String pwd = editText2.getText().toString();
        if (pwd.isEmpty()) {
            ToastUtil.showToast(this, "请输入密码");
            return;
        }
        progressFl.setVisibility(View.VISIBLE);
        MainApplication.getChatHelper().login(user, pwd, new LoginCallBack() {
            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressFl.setVisibility(View.GONE);
                        ToastUtil.showToast(getBaseContext(), "登录成功");
                        UserBean userBean = new UserBean();
                        userBean.setChatId(user);
                        userBean.setNickName(user);
                        userBean.setUserId(user);
                        userBean.setChatPwd(pwd);
                        UserHelper.login(userBean);
                        startActivity(new Intent(getBaseContext(), MainActivity.class));
                        finish();
                    }
                });

            }

            @Override
            public void onError(int code, final String message) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressFl.setVisibility(View.GONE);
                        ToastUtil.showToast(getBaseContext(), "登录失败：" + message);
                    }
                });

            }
        });
    }
}
